package com.app.couponsharelife.ui.main.me.profit.record
import android.content.Context
import android.graphics.Color
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.ui.main.me.profit.fragment.OfflineProfitFragment
import com.app.couponsharelife.ui.main.me.profit.fragment.OnlineProfitFragment
import com.app.couponsharelife.ui.view.ScaleTransitionPagerTitleView
import com.app.couponsharelife.utils.AppUtils
import kotlinx.android.synthetic.main.activity_profit_record.*
import kotlinx.android.synthetic.main.base_title_white.*
import kotlinx.android.synthetic.main.pickerview_time_selecte.view.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by XCT on 2019/2/21
 * 已结算收益明细列表
 */
class ProfitRecordActivity : BaseActivity() {
    private var titleList:ArrayList<String>?=null
    private var commonNavigator: CommonNavigator?=null
    private var pvTime: TimePickerView? = null
    private var pageIndex=0
    private var mPagers = arrayListOf<Fragment>()
    private val simpleDateFormat = SimpleDateFormat("yyyy-MM", Locale.getDefault())
    private var time=""
    override val viewLayoutId: Int get() = R.layout.activity_profit_record
    override fun initViews() {
        time=simpleDateFormat.format(System.currentTimeMillis())
        AppUtils.initImmersionBar(this,true)
        basetitle_title.text="已结算明细"
        basetitle_llRight.visibility=View.VISIBLE
        basetitle_llclose.setOnClickListener { finish() }
        basetitle_llRight.setOnClickListener {
            timePickers()
        }
        setViewPager()
    }
    private fun setViewPager(){
        titleList= ArrayList()
        titleList!!.add("线上收益")
        titleList!!.add("线下收益")
        mPagers.add(OnlineProfitFragment.newInstance())
        mPagers.add(OfflineProfitFragment.newInstance())
        profitRecord_viewpager.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            override fun getItem(position: Int): Fragment {
                return mPagers[position]
            }

            override fun getCount(): Int {
                return titleList!!.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return titleList!![position]
            }
        }
        commonNavigator= CommonNavigator(this)
        commonNavigator!!.isAdjustMode=true
        commonNavigator!!.adapter=object : CommonNavigatorAdapter(){
            override fun getTitleView(p0: Context, p1: Int): IPagerTitleView {
                val colorTransitionPagerTitleView= ScaleTransitionPagerTitleView(p0,1f)
                colorTransitionPagerTitleView.normalColor = resources.getColor(R.color.gray99)
                colorTransitionPagerTitleView.textSize=16f
                colorTransitionPagerTitleView.paint.isFakeBoldText=true
                colorTransitionPagerTitleView.selectedColor = resources.getColor(R.color.act_main_bot_nav_select_text_color)
                colorTransitionPagerTitleView.text = titleList!![p1]
                colorTransitionPagerTitleView.setOnClickListener { profitRecord_viewpager.currentItem = p1 }
                return colorTransitionPagerTitleView
            }

            override fun getCount(): Int {
                return titleList!!.size
            }

            override fun getIndicator(p0: Context?): IPagerIndicator? {
                val indicator= LinePagerIndicator(p0)
                indicator.setColors(resources.getColor(R.color.act_main_bot_nav_select_text_color))
                indicator.mode=LinePagerIndicator.MODE_WRAP_CONTENT
                return indicator
            }
        }
        profitRecord_viewpager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                pageIndex=position
            }
        })
        profitRecord_tablayout.navigator=commonNavigator
        ViewPagerHelper.bind(profitRecord_tablayout, profitRecord_viewpager)
    }
    fun timePickers() {
        time=simpleDateFormat.format(System.currentTimeMillis())
        val selectedDate = Calendar.getInstance()
        val startDate = Calendar.getInstance()
        val endDate = Calendar.getInstance()
        startDate.set(2016, 0, 1)
        endDate.set(2030, 11, 31)
        pvTime = TimePickerBuilder(this, OnTimeSelectListener { _, _ ->

        })
        .setDate(selectedDate)
        .setRangDate(startDate, endDate)
        .setLayoutRes(R.layout.pickerview_time_selecte) { v ->
            v.pickerview_tvQX.setOnClickListener { pvTime!!.dismiss() }
            v.pickerview_tvWC.setOnClickListener{
                refreshData()
                pvTime!!.dismiss()
            }
        }.setTimeSelectChangeListener { date ->
            time=simpleDateFormat.format(date.time)
        }
        .isCyclic(true)
        .setType(booleanArrayOf(true, true, false, false, false, false))
        .setLabel("年", "月", null, null, null, null)
        .setOutSideCancelable(true)  //点击外部区域关闭窗口
        //.setTextXOffset(-20, 0, 20, 0, 0, 0)
        .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
        .setDividerColor(Color.parseColor("#e2e1e1"))
        .isDialog(false)
        .setBackgroundId(Color.parseColor("#40333333"))
        .build()
        pvTime!!.show()
    }
    private fun refreshData(){
        if (0==pageIndex){
            (mPagers[0] as OnlineProfitFragment).refreshData(time)
        }else{
            (mPagers[1] as OfflineProfitFragment).refreshData(time)
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}
